package com.utils.tools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.utils.result.Ret;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: xufan
 **/
public class JsonResponse {
    /**
     * @author: xufan
     * @Date: 2021/8/5
     * @Description: 以JSON格式输出response内容
     */
    public static void responseOutWithJson(HttpServletResponse response, Ret responseObject) {
        //将实体对象转换为JSON Object转换
        JSONObject responseJSONObject = JSONObject.parseObject(JSON.toJSONString(responseObject));
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            response.getWriter().append(responseJSONObject.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
